#!/bin/bash

if [[ $1 == insercao ]]
then
	magicnumber=1
else
	if [[ $1 == busca ]]
	then
		magicnumber=0
	else
		echo "modo de uso: "$0 "[insercao/busca]"
		exit 0
	fi	
fi

cat $2 | grep -v consulta | grep -v col |grep -v linhas| grep -v , | \
	sed 's/tempo para insercao: //g' | sed 's/tempo para busca: //g' | tail -n 120 > tudo.txt

# 10 chaves
head -n 120 tudo.txt > 4chaves.txt
# 8 chaves
#head -n 240 tudo.txt | tail -n 120 > 8chaves.txt 
# 16 chaves
#tail -n 120 tudo.txt > 16chaves.txt

# apaga tabela, se existir
> tabela.txt

# faz a conta das medias e coloca no formato
# 4keys 8keys 16keys
for nregs in {16k,100k,250k,500k,1M,2M,4M,8M}
do
	a=$(grep $nregs -A 2 4chaves.txt | grep \\. | awk 'NR % 2 == '$magicnumber' {total = total + $1} END {print (total/5)*1000}')
#	b=$(grep $nregs -A 2 8chaves.txt | grep \\. | awk 'NR % 2 == '$magicnumber' {total = total + $1} END {print (total/5)*1000}')
#	c=$(grep $nregs -A 2 16chaves.txt | grep \\. | awk 'NR % 2 == '$magicnumber' {total = total + $1} END {print (total/5)*1000}')
	d="$a"
	echo $d >> tabela.txt
done

# cria arquivo com referencia de registros
cat << EOF > refs.txt
16000
100000
250000
500000
1000000
2000000
4000000
8000000
EOF
# cria arquivo com colunas de referencia
paste refs.txt tabela.txt > tabela2.txt
mv tabela2.txt tabela.txt

rm 4chaves.txt refs.txt
